﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Newtonsoft.Json.Linq;
using WeiboPlus.Service;
using WeiboPlus.Models;

namespace WeiboPlus.Sina
{
    public class SinaJTokenToUserMapping : MappingBase<JToken, SNSUser>
    {
        public SinaJTokenToUserMapping(IMappingFactory factory)
            : base(factory)
        {

        }

        public override SNSUser Map(JToken s)
        {
            try
            {
                var user = new SNSUser() { ServiceProvider = _factory.Account.ServiceProvider };
                if (s.SelectToken("id") != null)
                    user.Id = (long)s.SelectToken("id");
                //user.Account = _factory.Account;
                user.AccountID = _factory.Account.AccountID;
                if (s.SelectToken("screen_name") != null)
                    user.AccountName = (string)s.SelectToken("screen_name");
                if (s.SelectToken("screen_name") != null)
                    user.Name = (string)s.SelectToken("screen_name");
                if (s.SelectToken("province") != null)
                    user.Province = int.Parse((string)s.SelectToken("province"));
                if (s.SelectToken("city") != null)
                    user.City = int.Parse((string)s.SelectToken("city"));
                if (s.SelectToken("location") != null)
                    user.Location = (string)s.SelectToken("location");
                if (s.SelectToken("description") != null)
                    user.Description = (string)s.SelectToken("description");
                if (s.SelectToken("url") != null)
                    user.Url = (string)s.SelectToken("url");
                if (s.SelectToken("profile_image_url") != null)
                    user.ProfileImageUrl = (string)s.SelectToken("profile_image_url");
                if (s.SelectToken("avatar_large") != null)
                    user.LargeProfileImageUrl = (string)s.SelectToken("avatar_large");
                if (s.SelectToken("domain") != null)
                    user.Domain = (string)s.SelectToken("domain");
                if (s.SelectToken("gender") != null)
                    user.Gender = (string)s.SelectToken("gender")=="f"?"female":"male";

                if (s.SelectToken("followers_count") != null)
                    user.FollowersCount = (int)s.SelectToken("followers_count");
                if (s.SelectToken("friends_count") != null) 
                    user.FriendsCount = (int)s.SelectToken("friends_count");
                if (s.SelectToken("statuses_count") != null)
                    user.StatusesCount = (int)s.SelectToken("statuses_count");
                if (s.SelectToken("favourites_count") != null)
                    user.FavouritesCount = (int)s.SelectToken("favourites_count");

                if (s.SelectToken("following") != null)
                    user.Following = (bool)s.SelectToken("following");

                if (s.SelectToken("follow_me") != null)
                    user.Fans = (bool)s.SelectToken("follow_me");
                if (s.SelectToken("verified") != null)
                    user.Verified = (bool)s.SelectToken("verified");
                if (s.SelectToken("verified_reason") != null)
                    user.VerifiedReason = (string)s.SelectToken("verified_reason");
                if (s.SelectToken("status") != null)
                    user.LastStatusText = (string)s.SelectToken("status").SelectToken("text");
                return user;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                //return null;
                throw;
            }

            //throw new NotImplementedException();
        }
    }
}
